close all; clear all;clc
fs = 1000;                  % Sampling frequency
N = 1000;                   % Number of points
L = 512;                    % Optimnal filter order
F1 = 10;
F2 = 30;
delay = 6;  %Decorrelation delay,  white noise should have little correlation
[xn1, t1, x1] = sig_noise([F1],-8,N);   % 50 Hz sine;  SNR = -8 db
[xn2, t2, x2] = sig_noise([F2],-8,N);   % 50 Hz sine;  SNR = -8 db
t = [t1 t1(end)+t2];
x = [x1 x2];
xn = [xn1 xn2];
subplot(3,1,1);
plot(t, xn,'k');            % Plot unfiltered data
xlabel('Time(sec)','FontSize',14);
ylabel('x(t)','FontSize',14);
title('SNR -8 dB; 10 Hz & 30 Hz sines','FontSize',12);
Px = mean(xn.^2);
a = 0.25;
delta = a * (1/(10*L*Px));
xd = [x(delay:N*2) zeros(1,delay-1)];   % Delay signal to decorrelate broadband noise
[b,y,e] = lms(xd,x,delta,L);; %LMS algo
subplot(3,1,2);
plot(t,y,'k');              % Plot filtered data
xlabel('Time(sec)','FontSize',12);
ylabel('y(t)','FontSize',14);
title('After Adaptive Filtering','FontSize',14);
subplot(3,1,3);
[h,f] = pwelch(b,[],[],512,1000);
plot(f,h,'k');              % Plot Adaptive Filter spectra
xlabel('Frequency (Hz)','FontSize',12);
ylabel('|H(f)|','FontSize',14);
axis([0 100 0 1.2*max(h)])
title('Optimal Filter Freq Plot','FontSize',14);
